﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace RedditCrawler
{
    class RedditEntry
    {
        public RedditEntry()
        {

        }

        public RedditEntry(String s)
        {
            String[] parts = s.Split('|');
            if (parts.Length != 4) throw new Exception("Invalid RedditEntry line");
            imageURL = parts[0];
            linkText = parts[1];
            commentsURL = parts[2];
            subreddit = parts[3];
        }

        public String imageURL;
        public String linkText;
        public String commentsURL;
        public String subreddit;

        public override String ToString()
        {
            return SanitizeEntry(imageURL) + "|" + SanitizeEntry(linkText) + "|" + SanitizeEntry(commentsURL) + "|" + SanitizeEntry(subreddit);
        }

        public String ImageFilename()
        {
            String[] URLParts = imageURL.Split('/');
            return Utility.SanitizeText(linkText) + " " + URLParts[URLParts.Length - 1];
        }

        public String HTMLFilename()
        {
            String baseFilename = ImageFilename();
            baseFilename = baseFilename.Replace(".png", "");
            baseFilename = baseFilename.Replace(".jpg", "");
            baseFilename = baseFilename.Replace(".gif", "");
            return baseFilename + ".html";
        }

        static public void SaveGallery(String subredditName, List<RedditEntry> subredditEntries, Dictionary<String, List<RedditEntry>> allSubreddits, List<String> subredditOrdering, String[] templateLines)
        {
            StreamWriter writer = File.CreateText("Reddit/" + subredditName + ".html");
            foreach (String curLine in templateLines)
            {
                if (curLine.Contains("@@"))
                {
                    String processedLine = curLine;
                    processedLine = processedLine.Replace("@@SUBREDDIT_NAME", "/r/" + subredditName);
                    
                    if (processedLine.Contains("@@POSTS"))
                    {
                        processedLine = "";
                        foreach(RedditEntry curEntry in subredditEntries)
                        {
                            String s = "";
                            s += "<div class=\"post\">" + Environment.NewLine;
                            s += "<a href=\"" + curEntry.subreddit + "/" + curEntry.HTMLFilename() + "\">" + Environment.NewLine;
                            s += "<img alt=\"\" src=\"" + curEntry.subreddit + "/" + curEntry.ImageFilename() + "\" title=\"" + curEntry.linkText + "\" />" + Environment.NewLine;
                            s += "</a><div class=\"hover\">" + Environment.NewLine;
                            s += "<div class=\"arrows\"></div>" + Environment.NewLine;
                            s += "</div>" + Environment.NewLine;
                            s += "</div>" + Environment.NewLine;
                            processedLine += s;
                        }
                    }

                    if (processedLine.Contains("@@SUBREDDIT_LIST"))
                    {
                        processedLine = "";

                        foreach (String curSubredditName in subredditOrdering)
                        {
                            String s = "";
                            s += "<div class=\"usertext textbox caption \">" + Environment.NewLine;

                            String fontColor = "";
                            if (curSubredditName == subredditName)
                            {
                                fontColor = "COLOR=\"#800000\"";
                                
                            }
                            s += "<a href=\"" + curSubredditName + ".html\"><FONT " + fontColor + ">" + curSubredditName + " (" + allSubreddits[curSubredditName].Count.ToString() + ")" + "</font></a>" + Environment.NewLine;

                            s += "</div>" + Environment.NewLine;
                            s += "<div class=\"clear\">" + Environment.NewLine;
                            s += "</div>" + Environment.NewLine;
                            processedLine += s;
                        }
                    }

                    writer.WriteLine(processedLine);
                }
                else
                {
                    writer.WriteLine(curLine);
                }
            }
            writer.Close();
        }

        public void SaveHTML(String filename, List<RedditEntry> subredditEntries, String[] templateLines, Random random)
        {
            int myIndex = subredditEntries.FindIndex( delegate(RedditEntry entry) { return entry == this; } );

            int thumbnailCount = Math.Min(21, subredditEntries.Count - 1);

            List<RedditEntry> thumbnailEntries = new List<RedditEntry>();
            for (int thumbnailIndex = 0; thumbnailIndex < thumbnailCount; thumbnailIndex++)
            {
                RedditEntry randomEntry = subredditEntries[random.Next(0, subredditEntries.Count)];
                while (thumbnailEntries.Contains(randomEntry))
                {
                    randomEntry = subredditEntries[random.Next(0, subredditEntries.Count)];
                }
                thumbnailEntries.Add(randomEntry);
            }

            StreamWriter writer = File.CreateText(filename);
            foreach (String curLine in templateLines)
            {

                if (curLine.Contains("@@"))
                {
                    String processedLine = curLine;
                    processedLine = processedLine.Replace("@@TITLE", linkText);
                    processedLine = processedLine.Replace("@@SUBREDDIT_NAME", "/r/" + subreddit);
                    processedLine = processedLine.Replace("@@SUBREDDIT_LINK", "../" + subreddit + ".html");
                    processedLine = processedLine.Replace("@@COMMENTS", commentsURL);
                    processedLine = processedLine.Replace("@@IMAGE", ImageFilename());
                    processedLine = processedLine.Replace("@@ORIGINAL_IMAGE", imageURL);

                    processedLine = processedLine.Replace("@@PREV_LINK", subredditEntries[Utility.Mod(myIndex - 1, subredditEntries.Count)].HTMLFilename() );
                    processedLine = processedLine.Replace("@@NEXT_LINK", subredditEntries[Utility.Mod(myIndex + 1, subredditEntries.Count)].HTMLFilename());
                    
                    if(processedLine.Contains("@@THUMBNAILS"))
                    {
                        processedLine = "";
                        for (int thumbnailIndex = 0; thumbnailIndex < thumbnailCount; thumbnailIndex++)
                        {
                            RedditEntry curEntry = thumbnailEntries[thumbnailIndex];
                            processedLine += "<div class=\"thumb\"><a href=\"" + curEntry.HTMLFilename() + "\"><img src=\"" + curEntry.ImageFilename() + "\" title=\"" + curEntry.linkText + "\" /></a></div>" + Environment.NewLine;
                        }
                    }

                    writer.WriteLine(processedLine);
                }
                else
                {
                    writer.WriteLine(curLine);
                }
            }
            writer.Close();
        }

        private String SanitizeEntry(String s)
        {
            return s.Replace("|", "-");
        }
    };

        
}
